package Schedulers;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import threadedSimulator.Cpu;

import Processes.Processes;

//The scheduler will be called each time the CPU receives a system timer 
//interrupt or a process makes a system call. It will determine what event 
//activated it and then determine which runnable process to execute next 
//(don't forget about the context switch). You should implement schedulers 
//		for the round-robin, priority, and lottery scheduling policies.

public class lotteryScheduler extends Scheduler {
	
 
	
	private Random my_rand = new Random(17);
	
	public lotteryScheduler(Cpu the_cpu) {
		super(the_cpu);
	}
	
	public void addProcess(Processes the_process) {
		if (!my_list_of_processes.isEmpty()) {
			my_list_of_processes.add(my_rand.nextInt(100)  % my_list_of_processes.size() + 1, 
					 the_process);
		} else {
			my_list_of_processes.add(the_process);
		}
		
	}

	@Override
	public void haltCurrentProcess() {
		Processes last = my_list_of_processes.remove(0);
		last.running = false;
		if (!last.has_finished) {
			addProcess(last);
		}
		super.my_cpu.addToOutput(toString(), "Scheduler");
		my_list_of_processes.get(0).running = true;
		my_list_of_processes.get(0).run();
	}

	@Override
	public boolean systemCall(Thread the_input) {
		// TODO Auto-generated method stub
		return false;
	}
	
}
